package exams.concurrent;

public class VolatileExample {

    private volatile boolean flag = false;

    // Never ending
    // private boolean flag = false;

    public static void main(String[] args) {
        VolatileExample volatileExample = new VolatileExample();
        volatileExample.startTask();
        volatileExample.monitorTask();
    }

    public void startTask() {
        (new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                flag = true;
            }
        })).start();
    }

    public void monitorTask() {
        (new Thread(new Runnable() {
            @Override
            public void run() {
                while (!flag) {

                }
                System.out.println("Flag is now true. Task can proceed.");
            }
        })).start();
    }


}
